﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Api.Filter;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using System.ComponentModel;

namespace SmartConference.Api.Controllers
{
    /// <summary>
    /// 会议室管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "会议管理")]
    public class MeetingRoomController : ControllerBase
    {
        private readonly IMeetingRoomServices meetingRoomServices;
        private readonly ExportHelper<MeetingRoom> exportHelper;

        public MeetingRoomController(IMeetingRoomServices meetingRoomServices, ExportHelper<MeetingRoom> exportHelper)
        {
            this.meetingRoomServices = meetingRoomServices;
            this.exportHelper = exportHelper;
        }

        /// <summary>
        /// 获取所有会议室
        /// </summary>
        /// <param name="RoomName">会议室名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<MeetingRoom>>> ShowMeetRoom(string? RoomName, [DefaultValue(1)] int pageIndex, [DefaultValue(10)] int pageSize)
        {
            try
            {
                return await meetingRoomServices.ShowMeetRoom(RoomName, pageIndex, pageSize);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有会议室（无条件）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<IList<MeetingRoom>>> GetMeetRoom()
        {
            try
            {
                return await meetingRoomServices.GetMeetRoom();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 新增会议室
        /// </summary>
        /// <param name="meetingRoom">会议室实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddMeetRoom(AddMeetingRoomDTO meetingRoom)
        {
            try
            {
                return await meetingRoomServices.AddMeetRoom(meetingRoom);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除会议室
        /// </summary>
        /// <param name="Id">会议室Id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteMeetRoom(int Id)
        {
            try
            {
                return await meetingRoomServices.DeleteMeetRoom(Id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量删除会议室
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> BatchDeleteRoom(int[] ids)
        {
            try
            {
                return await  meetingRoomServices.BatchDeleteRoom(ids);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 编辑会议室
        /// </summary>
        /// <param name="meetingRoom">会议室实体</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateMeetRoom(MeetingRoom meetingRoom)
        {
            return await meetingRoomServices.UpdateMeetRoom(meetingRoom);
        }

        /// <summary>
        /// 导出会议室信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Export()
        {
            var rowName = "会议室信息";
            var titleName = "会议室信息";
            return File(exportHelper.Export<MeetingRoom>(rowName, titleName), System.Net.Mime.MediaTypeNames.Application.Octet, "会议室信息.xlsx");
        }
    }
}
